<template>
  <div id="area">
      <div id="select">
          <div class="sel">
            <div @click="pshow()">{{currProv ? currProv : '请选择省份'}}<div class="icon"></div></div>
            <div v-show="pro.show" :style="{opacity: pro.opacity, top: pro.top}">
                <div v-if="provs" class="sx" v-for="a in provs" @click="changeCity(a)">{{a.provinceName}}</div>
            </div>
        </div>
        <div class="sel">
            <div @click="cshow()">{{currCity ? currCity : '请选择城市'}}<div class="icon"></div></div>
            <div v-show="city.show" :style="{opacity: city.opacity, top: city.top}">
                <div class="sx" v-for="a in cityList" @click="sure(a)">{{a.name}}</div>
            </div>
        </div>
      </div>
      <div id="census" class="sel">
          <div @click="ceshow()">{{this.currSei === null ? '户籍类型' : this.type[this.currSei]}}<div class="icon"></div></div>
          <div v-show="census.show" :style="{opacity: census.opacity, top: census.top}">
                <div class="sx" v-for="(a, i) in type" @click="csure(i)">{{a}}</div>
            </div>
      </div>
  </div>
</template>
<script>
import { Get, Post } from "../../../server/ajax"
export default {
  data() {
    return {
      pro: {
        show: false,
        opacity: 0,
        top: 1.5 * parseFloat(window.getComputedStyle(document.documentElement)["fontSize"]) + "px"
      },
      city: {
        show: false,
        opacity: 0,
        top: 1.5 * parseFloat(window.getComputedStyle(document.documentElement)["fontSize"]) + "px"
      },
      provs: null,
      cities:null,
      census: {
        show: false,
        opacity: 0,
        top: 1.5 * parseFloat(window.getComputedStyle(document.documentElement)["fontSize"]) + "px"
      },
      type: ['本地城镇', '本地农村', '外地城镇', '外地农村'],
      area: [{"name":"北京市","city":["北京市"]},{"name":"天津市","city":["天津市"]},{"name":"河北省","city":["石家庄市","唐山市","秦皇岛市","邯郸市","邢台市","保定市","张家口","承德市","沧州市","廊坊市","衡水市"]},{"name":"山西省","city":["太原市","大同市","阳泉市","长治市","晋城市","朔州市","忻州市","吕梁市","晋中市","临汾市","运城市"]},{"name":"内蒙古","city":["呼和浩特市","包头市","乌海市","赤峰市","呼伦贝尔市","兴安盟","通辽市","锡林郭勒盟","乌兰察布盟","伊克昭盟","巴彦淖尔盟","阿拉善盟"]},{"name":"辽宁省","city":["沈阳市","大连市","鞍山市","抚顺市","本溪市","丹东市","锦州市","营口市","阜新市","辽阳市","盘锦","铁岭市","朝阳市","葫芦岛市"]},{"name":"吉林省","city":["长春市","吉林市","四平","辽源市","通化市","白山市","松原市","白城市","延边朝鲜族自治州"]},{"name":"黑龙江省","city":["哈尔滨市","齐齐哈尔市","鹤岗市","双鸭山","鸡西市","大庆市","伊春市","牡丹江市","佳木斯市","七台河市","黑河市","绥化市","大兴安岭地区"]},{"name":"上海市","city":["上海市"]},{"name":"江苏省","city":["南京市","苏州市","无锡市","常州市","镇江市","南通市","泰州市","扬州市","盐城市","连云港市","徐州市","淮安市","宿迁市"]},{"name":"浙江省","city":["杭州市","宁波市","温州市","嘉兴市","湖州市","绍兴市","金华市","衢州市","舟山市","台州市","丽水市","其他市"]},{"name":"安徽省","city":["合肥市","芜湖市","蚌埠市","淮南市","马鞍山市","淮北市","铜陵市","安庆市","黄山市","滁州市","阜阳市","宿州市","巢湖市","六安市","亳州市","池州市","宣城市","其他市"]},{"name":"福建省","city":["福州市","厦门市","莆田市","三明市","泉州市","漳州市","南平市","龙岩市","宁德市"]},{"name":"江西省","city":["南昌市","景德镇市","萍乡市","九江市","新余市","鹰潭市","赣州市","吉安市","宜春市","抚州市","上饶市"]},{"name":"山东省","city":["济南市","青岛市","淄博市","枣庄市","东营市","烟台市","潍坊市","济宁市","泰安市","威海市","日照市","莱芜市","临沂市","德州市","聊城市","滨州市","菏泽市"]},{"name":"河南省","city":["郑州市","开封市","洛阳市","平顶山市","安阳市","鹤壁市","新乡市","焦作市","濮阳市","许昌市","漯河市","三门峡市","南阳市","商丘市","信阳市","周口市","驻马店市","焦作市"]},{"name":"湖北省","city":["武汉市","黄石市","十堰市","荆州市","宜昌市","襄樊市","鄂州市","荆门市","孝感市","黄冈市","咸宁市","随州市","恩施土家族苗族自治州","仙桃市","天门市","潜江市","神农架林区"]},{"name":"湖南省","city":["长沙市","株洲市","湘潭市","衡阳市","邵阳市","岳阳市","常德市","张家界市","益阳市","郴州市","永州市","怀化市","娄底市","湘西土家族苗族自治州"]},{"name":"广东省","city":["广州市","深圳市","东莞市","中山市","潮州市","揭阳市","云浮市","珠海市","汕头市","韶关市","佛山市","江门市","湛江市","茂名市","肇庆市","惠州市","梅州市","汕尾市","河源市","阳江市","清远市"]},{"name":"广西","city":["南宁市","柳州市","桂林市","梧州市","北海市","防城港市","钦州市","贵港市","玉林市","百色市","贺州市","河池市","来宾市","崇左市","其他市"]},{"name":"海南省","city":["海口市","三亚市","五指山市","琼海市","儋州市","文昌市","万宁市","东方市","澄迈县","定安县","屯昌县","临高县","白沙黎族自治县","昌江黎族自治县","乐东黎族自治县","陵水黎族自治县","保亭黎族苗族自治县","琼中黎族苗族自治县"]},{"name":"重庆市","city":["重庆市"]},{"name":"四川省","city":["成都市","自贡市","攀枝花市","泸州市","德阳市","绵阳市","广元市","遂宁市","内江市","乐山市","南充","眉山市","宜宾市","广安市","达州市","雅安市","巴中市","资阳市","阿坝藏族羌族自治州","甘孜藏族自治州","凉山彝族自治州"]},{"name":"贵州省","city":["贵阳市","六盘水市","遵义市","安顺市","铜仁地区","毕节地区","黔西南布依族苗族自治州","黔东南苗族侗族自治州","黔南布依族苗族自治州"]},{"name":"云南省","city":["昆明市","曲靖市","玉溪市","保山市","昭通市","丽江市","普洱市","临沧市","德宏傣族景颇族自治州","怒江傈僳族自治州","迪庆藏族自治州","大理白族自治州","楚雄彝族自治州","红河哈尼族彝族自治州","文山壮族苗族自治州","西双版纳傣族自治州"]},{"name":"西藏","city":["拉萨市","那曲地区","昌都地区","林芝地区","山南地区","日喀则地区","阿里地区"]},{"name":"陕西省","city":["西安市","铜川市","宝鸡市","咸阳市","渭南市","延安市","汉中市","榆林市","安康市","商洛市"]},{"name":"甘肃省","city":["兰州市","嘉峪关市","金昌市","白银市","天水市","武威市","酒泉市","张掖市","庆阳市","平凉市","定西市","陇南市","临夏回族自治州","甘南藏族自治州"]},{"name":"青海省","city":["西宁市","海东地区","海北藏族自治州","海南藏族自治州","黄南藏族自治州","果洛藏族自治州","玉树藏族自治州","海西蒙古族藏族自治州"]},{"name":"宁夏","city":["银川市","石嘴山市","吴忠市","固原市","中卫市"]},{"name":"新疆","city":["乌鲁木齐市","克拉玛依市","吐鲁番地区","哈密地区","和田地区","阿克苏地区","喀什地区","克孜勒苏柯尔克孜自治州","巴音郭楞蒙古自治州","昌吉回族自治州","博尔塔拉蒙古自治州","石河子","阿拉尔","图木舒克","五家渠","伊犁哈萨克自治州"]},{"name":"台湾省","city":["台北市","新北市","桃园市","台中市","台南市","高雄市"]},{"name":"澳门","city":["澳门"]},{"name":"香港","city":["香港"]}],
      currProv: null,
      currCity: null,
      cityList: [],
    //   currCity: {"name":"北京市","city":["北京市"]},
      currIndex: 0,
      currSei: 0,
      pid: null,
      isInit: true
    };
  },
  props: ['cname'],
  methods: {
    pshow() {
      if (!this.pro.show) {
        this.pro.show = true;
        setTimeout(function() {
            this.pro.opacity = 1;
            this.pro.top = 1.2 * parseFloat(window.getComputedStyle(document.documentElement)["fontSize"]) + "px";
        }.bind(this),10);
      }
      else{
          this.pro = {
              show: false,
              opacity: 0,
              top: 1.5 * parseFloat(window.getComputedStyle(document.documentElement)["fontSize"]) + "px"
          }
      }
    },
    cshow() {
        if (!this.city.show) {
        this.city.show = true;
        setTimeout(function() {
            this.city.opacity = 1;
            this.city.top = 1.2 * parseFloat(window.getComputedStyle(document.documentElement)["fontSize"]) + "px";
        }.bind(this),10);
      }
      else{
          this.city = {
              show: false,
              opacity: 0,
              top: 1.5 * parseFloat(window.getComputedStyle(document.documentElement)["fontSize"]) + "px"
          }
      }
    },
    ceshow() {
        if (!this.census.show) {
        this.census.show = true;
        setTimeout(function() {
            this.census.opacity = 1;
            this.census.top = 1.2 * parseFloat(window.getComputedStyle(document.documentElement)["fontSize"]) + "px";
        }.bind(this),10);
      }
      else{
          this.census = {
              show: false,
              opacity: 0,
              top: 1.5 * parseFloat(window.getComputedStyle(document.documentElement)["fontSize"]) + "px"
          }
      }
    },
    changeCity(a){
        this.pshow()
        this.currProv = a.provinceName
        this.$emit('prov', a)
        this.pid = a.id
        this.city.show = false
        this.upList()
    },
    sure (a) {
        this.cshow()
        this.currCity = a.name
        this.$emit('city', a)
    },
    csure(a){
        this.ceshow()
        this.currSei = a
        this.$emit('house', a + 1)
    },
    init() {
        var _this = this
        this.citys.forEach(e => {
            if(e.name == _this.cname){
                _this.currCity = e.name
                // console.log(e.name)
                _this.pid = e.provinceId
                _this.$emit('city', e)
            }
        })
        this.provs.forEach(e => {
            if(e.id == _this.pid){
                _this.currProv = e.provinceName
                _this.$emit('prov', e)
            }
        })
    },
    upList(){
        var _this = this
        this.cityList = []
        this.citys.forEach(e => {
            if(e.provinceId == _this.pid){
                _this.cityList.push(e)
            }
        })
        var em
        if(this.cityList.length){
            em = this.cityList[0]
            if(this.isInit){
                this.cityList.forEach(e => {
                    if(e.name == _this.cname){
                        em = e
                    }
                })
            }
        }
        else{
            em = {
                name: ''
            }
        }
        this.currCity = em.name
        this.$emit('city', em)
    }
  },
  mounted() {
    Get('https://grsb.aiyuangong.com/pis/api/customer/customerorders/get-ins-city-with-pinyin', function(data){
        this.provs = data.provinces
        this.citys = data.cities
        this.init()
        this.upList()
    }.bind(this))
  },
  activated() {
    setTimeout(function(){
      if(this.provs) {
        this.isInit = true 
        this.init()
        this.upList()
      }
    }.bind(this), 10)
  }
};
</script>
<style scoped>
#area {
  margin-top: 0.9rem;
}
#select{
    display: flex;
    justify-content: space-around;
    padding: 0 0.18rem;
}
.sel {
  position: relative;
  width: 3.3rem;
  height: 1.1rem;
  border: 0.01rem solid #ccc;
  font-size: 0.32rem;
  line-height: 1.1rem;
  text-indent: 0.4rem;
  color: #333;
}
.icon {
  position: absolute;
  top: 0.45rem;
  right: 0.4rem;
  width: 0.33rem;
  height: 0.19rem;
  background-image: url("~@/assets/scounter/sicon.png");
  background-size: cover;
}
.sel > div:nth-child(2) {
  position: absolute;
  z-index: 1;
  top: 1.2rem;
  width: 100%;
  max-height: 4rem;
  overflow: auto;
  border: 0.01rem solid #ccc;
  transition: all 0.3s;
  background-color: #fff;
}
#census{
    width: 6.9rem;
    margin: 0.38rem auto 0;
}
.sx{
    border-bottom: 0.01rem solid #e2e2e2;
}
</style>
